package com.ruoyi.web.controller.vantController.customerController;

import com.ruoyi.common.constant.Constants;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.AliyunSmsUtils;
import com.ruoyi.system.domain.customer.CustomerLogin;
import com.ruoyi.system.domain.customer.ZeroYuanService;
import com.ruoyi.system.service.customer.ZeroYuanServiceService;
import com.ruoyi.system.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/api/service")
public class ApiZeroYuanServiceController extends BaseController {

    @Autowired
    private ZeroYuanServiceService zeroYuanServiceService;
    @Autowired
    private HttpServletRequest request;

    @Autowired
    private TokenUtils tokenUtils;

    @Autowired
    private RedisCache redisCache;

    @Value("${ruoyi.telphone}")
    private String telPhone;

    @GetMapping(value = "/list")
    public List<ZeroYuanService> list(ZeroYuanService zeroYuanService) {
        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
        zeroYuanService.setCustomerId(customerLogin.getCustomerId());
        List<ZeroYuanService> zeroYuanServices = zeroYuanServiceService.selectZeroYuanServiceList(zeroYuanService);
        return zeroYuanServices;
    }

    /**
     * 新增零元质检
     */
    @PostMapping(value = "/add")
    public AjaxResult add(@RequestBody ZeroYuanService zeroYuanService)
    {

        if (UserConstants.NOT_UNIQUE.equals(zeroYuanServiceService.checkcustomerIdUnique(zeroYuanService)))
        {
            return AjaxResult.error("已提交质检服务");
        }

        StringBuilder stringBuilder = new StringBuilder();
        Random random = new Random();
        for (int i=0;i<8;i++){
            stringBuilder.append(random.nextInt(10));
        }
        String num =stringBuilder.toString();
        System.err.println("**********88888888888888"+num);
        zeroYuanService.setZeroNumber(num);

        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
        zeroYuanService.setCustomerId(customerLogin.getCustomerId());

        zeroYuanService.setTeamStatus("2");

        return toAjax(zeroYuanServiceService.insertZeroYuanService(zeroYuanService));
    }

    @PutMapping(value = "/edit")
    public AjaxResult edit(@RequestBody ZeroYuanService zeroYuanService) {
        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
        zeroYuanService.setCustomerId(customerLogin.getCustomerId());

        zeroYuanService.setTeamStatus("1");
        int count = zeroYuanServiceService.updateZeroYuan(zeroYuanService);
        if (count > 0) {
            // 创建短信验证码类对象
            AliyunSmsUtils smsUtils = new AliyunSmsUtils();
            try {
                //发送短信
                SendSmsResponse response = smsUtils.sendSms(telPhone,null);
            } catch (ClientException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return toAjax(count);
    }


    @GetMapping(value = "getInfo/{customerId}")
    public AjaxResult getInfo(@PathVariable("customerId") Long customerId)
    {
        return AjaxResult.success(zeroYuanServiceService.customerIdServiceById(customerId));
    }

    //获取用户手机号
    @PostMapping(value = "/getLoginName")
    public AjaxResult getLoginName() {
        CustomerLogin loginCustomer = tokenUtils.getLoginCustomer(request);
        String loginName = loginCustomer.getLoginName();
        if (loginName != null && !"".equals(loginName)) {
            return AjaxResult.success(loginName);
        }
        return AjaxResult.error("获取用户手机号失败");
    }

}
